package com.rerun.phone.park;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rerun.common.anno.ResponseExceptionReturn;
import com.rerun.common.bean.Rest;
import com.rerun.common.controller.SuperController;
import com.rerun.entity.Job;
import com.rerun.entity.JobTag;
import com.rerun.entity.vo.JobDetailVo;
import com.rerun.enums.JobStateEnum;
import com.rerun.service.IJobService;
import com.rerun.service.JobTagService;
import com.rerun.vo.park.JobValidVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 和职位相关的API
 *
 * @author Administrator
 */
@Api(value="园长端-职位相关",description="园长端-职位相关")
@RestController
@RequestMapping(value = "/parkApi/job")
public class ParkJobController extends SuperController{
    /**
     * The constant logger.
     */
    public static final Logger logger = Logger.getLogger(ParkJobController.class);
    /**
     * The Job service.
     */
    @Autowired
	IJobService iJobService;
    /**
     * The Job tag service.
     */
    @Autowired
	JobTagService jobTagService;

    /**
     * 添加职位
     *
     * @param jobValidVo the job valid vo
     * @return rest rest
     */
    @ApiOperation(value = "添加职位", httpMethod = "POST",notes = "园长添加招聘职位")
	@RequestMapping(value = "/add",method=RequestMethod.POST)
    @ResponseExceptionReturn
	public Rest addJob(@ModelAttribute JobValidVo jobValidVo) {
		Job job = new Job();
		try {
			BeanUtils.copyProperties(job, jobValidVo);
			logger.info(job.toString());
		} catch (IllegalAccessException | InvocationTargetException e) {
			logger.error("bean属性拷贝异常,具体原因:" + e.getMessage());
			return Rest.failure("添加职位失败，系统繁忙");
		}
		List<JobTag> jobTags = jobValidVo.getJobTags();
		if (jobTags != null && jobTags.size() > 0)
			logger.info(jobTags.toString());

		boolean result = iJobService.addJob(job, jobTags);
		if (result)
			return Rest.ok("添加成功");

		return Rest.failure("error");
	}

    /**
     * 编辑职位
     *
     * @param jobValidVo the job valid vo
     * @return rest rest
     */
    @ApiOperation(value = "编辑职位", httpMethod = "POST",notes = "园长编辑招聘职位")
	@RequestMapping(value = "/edit")
    @ResponseExceptionReturn
	public Rest editJob(@ModelAttribute JobValidVo jobValidVo) {
		Job job = new Job();
		try {
			BeanUtils.copyProperties(job, jobValidVo);
			logger.info(job.toString());
		} catch (IllegalAccessException | InvocationTargetException e) {
			logger.error("bean属性拷贝异常,具体原因:" + e.getMessage());
			return Rest.failure("添加职位失败，系统繁忙");
		}
		List<JobTag> jobTags = jobValidVo.getJobTags();
		
		boolean result = iJobService.editJob(job, jobTags);
		if (result)
			return Rest.ok("编辑成功");
		return Rest.failure("error");
	}

    /**
     * 职位详情
     *
     * @param id the id
     * @return rest rest
     */
    @ApiOperation(value = "职位详情", httpMethod = "GET",notes = "获取职位详情")
	@RequestMapping(value = "/detail/{id}")
    @ResponseExceptionReturn
	public Rest jobDetail(@ApiParam(value="职位ID",required=true) @PathVariable String id) {
		if(StringUtils.isNotEmpty(id)) {
			JobDetailVo detailVo = iJobService.getJobDetailAndYoyoPark(id);
			return Rest.okData(detailVo);
		}
		return Rest.failure("参数不正确");
	}

    /**
     * 职位标签
     *
     * @return rest rest
     */
    @ApiOperation(value = "职位标签", httpMethod = "GET",notes = "获取职位标签")
	@RequestMapping(value = "/tag")
    @ResponseExceptionReturn
	public Rest jobWeal() {
		EntityWrapper<JobTag> wrapper = new EntityWrapper<>();
		List<JobTag> jobs = jobTagService.selectList(wrapper);
		return Rest.okData(jobs);
	}
	/**
	 * 职位管理
	 * @param parkId
	 * @param pageNumber
	 * @return
	 */
	@ApiOperation(value = "职位管理", httpMethod = "GET",notes = "职位管理")
	@RequestMapping(value="/list/{parkId}/{pageNumber}")
	@ResponseExceptionReturn
	public Rest list(@PathVariable String parkId, @PathVariable Integer pageNumber) {
		Page<Job> page = getPage(pageNumber);
		EntityWrapper<Job> wrapper = new EntityWrapper<>();
		wrapper.where("parkId={0} and status!={1}", parkId,JobStateEnum.delete.getKey());
		page = iJobService.selectPage(page, wrapper);
		return Rest.okData(page.getRecords());
	}
	
	@ApiOperation(value = "职位状态", notes = "修改职位状态", httpMethod = "GET")
	@RequestMapping(value = "/changeStatus/{jobId}/{status}")
	@ResponseExceptionReturn
	public Rest forTheInterView(@ApiParam(name = "jobId",required = true, value = "修改职位Id")@PathVariable String jobId,@ApiParam(name = "status",required = true, value = "0(正常状态),4(停止招聘/停止发布),5(删除)")@PathVariable String status) {
		try {
			Job arg0 = new Job();
			arg0.setStatus(Long.parseLong(status));
			Job job = iJobService.selectById(jobId);
			arg0.setParkId(job.getParkId());
			EntityWrapper<Job> arg1 = new EntityWrapper<>();
			arg1.eq("id", jobId);
			iJobService.update(arg0, arg1);
			return Rest.ok("ok");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Rest.failure("error");
	}
}
